﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

#endregion

using System;

namespace ScrumTable.Common.Collections
{
    /// <summary>
    /// Gets a triple of three value instances.
    /// </summary>
    public class Triple<TLeft, TMiddle, TRight> : Pair<TLeft, TRight>
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets/sets the internal Middle value (provides a SETTER).
        /// </summary>
        protected TMiddle MiddleInternal;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the middle value associated with the pair.
        /// </summary>
        /// <remarks>
        /// Setting the value of this property will throw a NotSupportedException.
        /// </remarks>
        public virtual TMiddle Middle
        {
            get { return MiddleInternal; }
            set { throw new NotSupportedException(); }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new Triple instance.
        /// </summary>
        public Triple(TLeft left, TMiddle middle, TRight right)
            : base(left, right)
        {
            MiddleInternal = middle;
        }

        #endregion


        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="left">Specifies the left object.</param>
        /// <param name="right">Specifies the right object.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Triple<TLeft, TMiddle, TRight> left, Triple<TLeft, TMiddle, TRight> right)
        {
            // If both are null, or both are same instance, return true.
            if (ReferenceEquals(left, right))
            {
                return true;
            }

            // If one is null, but not both, return false.
            if ((object)left == null || (object)right == null)
            {
                return false;
            }

            // Return true if the fields match:
            return left.Equals(right);
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="left">Specifies the left object.</param>
        /// <param name="right">Specifies the right object.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Triple<TLeft, TMiddle, TRight> left, Triple<TLeft, TMiddle, TRight> right)
        {
            return !(left == right);
        }

        /// <summary>
        /// Determines whether the specified IDataObject is equal to the current object.
        /// </summary>
        /// <param name="toCompare">Parameter to check.</param>
        /// <returns>Returns true if the objects are equal.</returns>
        public override bool Equals(object toCompare)
        {
            // If parameter cannot be cast to DomainBase return false.
            return Equals(toCompare as Triple<TLeft, TMiddle, TRight>);
        }

        /// <summary>
        /// Determines whether the specified IDataObject is equal to the current object.
        /// </summary>
        /// <param name="obj">Parameter to check.</param>
        /// <returns>Returns true if the objects are equal.</returns>
        public bool Equals(Triple<TLeft, TMiddle, TRight> obj)
        {
            // If parameter is null return false:
            if ((object)obj == null) // force compiler to use Object::operator==()
                return false;

            // Return true if the fields match:
            return (Equals(obj.Left, Left) && Equals(obj.Middle, Middle) && Equals(obj.Right, Right));
        }

        /// <summary>
        /// Returns the hash code for this IDataObject.
        /// </summary>
        /// <returns>Returns the computed hash value.</returns>
        public override int GetHashCode()
        {
            int hashCode = base.GetHashCode();
            
            if ((object)Middle != null)
                hashCode += Middle.GetHashCode();

            return hashCode;
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
